package view;

import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import model.Contact;

/**
 *
 * @author Sacha MASSON
 */
public class ContactDisplay extends JPanel implements ActionListener {

    public JLabel image;
    public JLabel l_lname;
    public JLabel l_fname;
    public JLabel l_address;
    public JLabel l_personalPhone;
    public JLabel l_proPhone;
    public JLabel l_email;
    public JLabel l_homePage;
    public JLabel l_other;
    public JTextField name;
    public JTextField firstName;
    public JTextField homeAddress;
    public JTextField personalPhone;
    public JTextField proPhone;
    public JTextField email;
    public JTextField homePage;
    public JTextField other;
    public JPanel panel;
    public JComboBox group;
    JFileChooser fc = new JFileChooser("img");
    private JButton button_Email;
    private JButton button_Home;
    private JButton button_Edit;
    private JButton button_Delete;
    private JButton browse;

    ArrayList<Contact> al;
    private Contact co;

    public ContactDisplay(ArrayList<Contact> a) {
        super();
        al = a;

        image = new JLabel();
        image.setIcon(new ImageIcon("img/default.gif"));
        image.setPreferredSize(new Dimension(100, 100));

        l_lname = new JLabel("Name :");
        name = new JTextField(15);
        name.setMinimumSize(new Dimension(80, 20));

        l_fname = new JLabel("First name :");
        firstName = new JTextField(15);
        firstName.setMinimumSize(new Dimension(80, 20));

        l_address = new JLabel("Address :");
        homeAddress = new JTextField(30);
        homeAddress.setMinimumSize(new Dimension(160, 20));

        l_personalPhone = new JLabel("Phone :");
        personalPhone = new JTextField(30);
        personalPhone.setMinimumSize(new Dimension(160, 20));

        l_proPhone = new JLabel("Pro phone :");
        proPhone = new JTextField(30);
        proPhone.setMinimumSize(new Dimension(160, 20));

        l_email = new JLabel("Email :");
        email = new JTextField(30);
        email.setMinimumSize(new Dimension(160, 20));
        button_Email = new JButton("Email him");
        button_Email.addActionListener(this);

        l_homePage = new JLabel("Home :");
        homePage = new JTextField(30);
        homePage.setMinimumSize(new Dimension(160, 20));

        l_other = new JLabel("Other :");
        other = new JTextField(30);
        other.setMinimumSize(new Dimension(160, 20));

        button_Home = new JButton("Visit the site");
        button_Home.addActionListener(this);

        button_Edit = new JButton("Edit");
        button_Edit.addActionListener(this);

        button_Delete = new JButton("Delete");
        button_Delete.addActionListener(this);

        browse = new JButton("Change picture");
        browse.addActionListener(this);

        //Test with GridBagLayout
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        //Image
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 2;
        c.insets = new Insets(5, 5, 5, 5);
        c.fill = GridBagConstraints.NONE;
        this.add(image, c);

        //Name
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridheight = 1;

        this.add(browse, c);

        //Name
        c.gridx = 3;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(name, c);

        c.gridx = 2;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_lname, c);

        //FName
        c.gridx = 3;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(firstName, c);

        c.gridx = 2;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_fname, c);

        //Address
        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(homeAddress, c);

        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_address, c);

        //Phone perso
        c.gridx = 1;
        c.gridy = 4;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(personalPhone, c);

        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_personalPhone, c);

        //Pro phone
        c.gridx = 1;
        c.gridy = 5;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(proPhone, c);

        c.gridx = 0;
        c.gridy = 5;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_proPhone, c);

        //Email
        c.gridx = 1;
        c.gridy = 6;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(email, c);

        c.gridx = 0;
        c.gridy = 6;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_email, c);

        c.gridx = 3;
        c.gridy = 6;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(button_Email, c);

        //Home
        c.gridx = 1;
        c.gridy = 7;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(homePage, c);

        c.gridx = 0;
        c.gridy = 7;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_homePage, c);

        c.gridx = 3;
        c.gridy = 7;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(button_Home, c);

        //Other
        c.gridx = 1;
        c.gridy = 8;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(other, c);

        c.gridx = 0;
        c.gridy = 8;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_other, c);

        //Groups
        String[] groupInt = {"Family", "Friends", "Work"};
        group = new JComboBox(groupInt);
        c.gridx = 1;
        c.gridy = 9;
        c.gridwidth = 2;
        c.gridheight = 2;
        this.add(group, c);

        //Buttons
        c.gridx = 1;
        c.gridy = 11;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(button_Edit, c);

        c.gridx = 2;
        c.gridy = 11;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(button_Delete, c);

        this.setVisible(true);
        this.repaint();
    }

    public void reset() {
        image.setIcon(new ImageIcon("img/default.gif"));
        name.setText("");
        firstName.setText("");
        personalPhone.setText("");
        proPhone.setText("");
        homeAddress.setText("");
        homePage.setText("");
        email.setText("");
        other.setText("");

    }

    public void update(Contact c) {
        co = c;

        image.setIcon(new ImageIcon(c.image));
        name.setText(c.getName());
        firstName.setText(c.getFirstName());
        personalPhone.setText(c.getPersonalPhone());
        proPhone.setText(c.getProfessionalPhone());
        homeAddress.setText(c.getHomeAddress());
        homePage.setText(c.getHomePage());
        email.setText(c.getEmail());
        other.setText(c.getOther());

    }

    private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {

        int res = fc.showOpenDialog(null);
        File file = fc.getSelectedFile();
        String path = fc.getSelectedFile().getAbsolutePath();
        image.setIcon(new ImageIcon(path));
        co.editPicture(path);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button_Home) {
            try {
                (Desktop.getDesktop()).browse(new URI(homePage.getText()));
            } catch (Exception ex) {
                Logger.getLogger(ContactDisplay.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (e.getSource() == button_Email) {
            try {
                (Desktop.getDesktop()).mail(new URI("mailto:" + email.getText()));
            } catch (Exception ex) {
                Logger.getLogger(ContactDisplay.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (e.getSource() == button_Edit) {

            co.editContact(name.getText(), firstName.getText(), homeAddress.getText(), personalPhone.getText(), proPhone.getText(), homePage.getText(), email.getText(), other.getText(), group.getSelectedIndex());
            JOptionPane.showMessageDialog(panel, "Contact successfully edited");
        }
        if (e.getSource() == button_Delete) {
            al.remove(co);
            JOptionPane.showMessageDialog(panel, "Contact successfully deleted");
        }
        if (e.getSource() == browse) {
            browseButtonActionPerformed(e);

        }
    }
}
